package class07;


import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class TreeMaxWidth2 {
    public static class Node{
        public int value;
        public Node left;
        public Node right;

        public Node(int data){
            value = data;
        }
    }

    public static int maxWidthUseMap(Node head){
        if (head==null)
            return 0;
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        HashMap<Node,Integer> levelMap = new HashMap<>();
        levelMap.put(head,1);
        int curLevel = 1;
        int curLevelNodes = 0;
        int max = 0;
        while (!queue.isEmpty()){
            Node cur = queue.poll();
            int curNodeLevel = levelMap.get(cur);
            if (cur.left!=null){
                levelMap.put(cur.left,curNodeLevel+1);
                queue.add(cur.left);
            }
            if (cur.right!=null){
                levelMap.put(cur.right,curNodeLevel+1);
                queue.add(cur.right);
            }
            if (curNodeLevel==curLevel){
                curLevelNodes++;
            }else {
                max = Math.max(max,curLevelNodes);
                curLevel++;
                curLevelNodes = 1;
            }
        }
        max = Math.max(max,curLevelNodes);
        return max;
    }

    public static int maxWidthNoMap(Node head){
        if (head == null)
            return 0;
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        Node curEnd = head;
        Node nextEnd = null;
        int max = 0;
        int curLevelNodes = 0;
        while (!queue.isEmpty()){
            Node cur = queue.poll();
            if (cur.left!=null){
                queue.add(cur.left);
                nextEnd = cur.left;
            }
            if (cur.right!=null){
                queue.add(cur.right);
                nextEnd=cur.right;
            }
            curLevelNodes++;
            if (cur == curEnd){
                max = Math.max(max,curLevelNodes);
                curLevelNodes=0;
                curEnd = nextEnd;
            }
        }
        return max;
    }

    public static Node generateRandomBST(int maxLevel,int maxValue){
        return generate(1,maxLevel,maxValue);
    }

    public static Node generate(int level,int maxLevel,int maxValue){
        if (level >maxLevel||Math.random()<0.5){
            return null;
        }
        Node head = new Node((int)(Math.random()*maxValue));
        head.left = generate(level+1,maxLevel,maxValue);
        head.right = generate(level+1,maxLevel,maxValue);
        return head;
    }

    public static void main(String[] args) {
        int maxLevel = 10;
        int maxValue = 100;
        int testTime = 100000;
        for (int i=0;i<testTime;i++){
            Node head = generateRandomBST(maxLevel,maxValue);
            if (maxWidthUseMap(head)!=maxWidthNoMap(head)){
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");
    }

}
